<?php
namespace Macaca\Module\Admin;

use Library\Filter;
use Macaca;
use Monkey;

class Page {

    /**
     * @var Monkey\App
     */
    public $app;

    private $modelPage; //'page_id', 'p_name', 'p_caption', 'content', 'keywords', 'description'
    private $modelPriKey;
    private $modelCacheKey = 'MacacaPage';

    /**
     * @param Monkey\App $app
     */
    public function __construct($app) {
        $this->app = $app;
        $this->modelPage = new Macaca\Model\Admin\Page($app);
        $this->modelPriKey = $this->modelPage->priKey;
    }

    /**
     * @param int $page
     * @param int $pageLength
     * @return array //`page_id`, `p_name`, `p_caption`
     */
    public function getPageList($page = 0, $pageLength = 10) {
        return $this->modelPage->findList($page, $pageLength);
    }

    /**
     * @return int
     */
    public function getCount() {
        $result = 0;
        $cache = $this->app->cache();
        if (!$cache->fetch($this->modelCacheKey . 'count', $result)) {
            $result = $this->modelPage->count();
            $cache->store($this->modelCacheKey . 'count', $result);
        }
        return $result;
    }

    /**
     * 获取一个空页面 //'page_id', 'p_name', 'p_caption', 'content', 'keywords', 'description'
     * @return mixed
     */
    public function getEmptyItem() {
        $data['page_id'] = '';
        $data['p_name'] = 'new_page';
        $data['p_caption'] = '新页面';
        $data['content'] = '';
        $data['keywords'] = 'Macaca 建站系统';
        $data['description'] = 'Macaca 建站系统';
        return $data;
    }

    public function getItemById($pageId) {
        $data = $this->modelPage->find($this->modelPriKey, $pageId);

        if ($data) {
            return $data;
        }
        else {
            return array();
        }
    }

    public function deleteItemById($pageId) {
        if ($this->modelPage->delete($this->modelPriKey, $pageId)) {
            $this->resetCache();
            return true;
        }
        else {
            return false;
        }
    }

    public function saveItem($data = array()) {
        $pk = $this->modelPriKey;
        if (!empty($data[$pk]) and !is_numeric($data[$pk])) {
            return false;
        }

        $dataTemp = $this->getEmptyItem();
        unset($dataTemp[$pk]);
        foreach ($dataTemp as $k => $v) {
            if (!isset($data[$k])) {
                return false;
            }
            $dataTemp[$k] = $data[$k];
        }

        $dataTemp['content'] = Filter::compressWhite($dataTemp['content']);
        $dataTemp['content'] = Filter::xssToHtml($dataTemp['content']);

        if (!empty($data[$pk])) {
            $result = $this->modelPage->updateByPriKey($data[$pk], $dataTemp);
        }
        else {
            $result = $this->modelPage->insert($dataTemp);
        }

        if ($result) {
            $this->resetCache();
        }

        return $result;
    }


    protected function resetCache() {
        $this->app->cache()->delete($this->modelCacheKey);
    }
} 